package com.whl.org.controller;


import com.whl.basic.exception.BusinessException;
import com.whl.basic.util.AjaxResult;
import com.whl.basic.util.ExcelUtil;
import com.whl.basic.util.PageList;
import com.whl.org.domain.Shop;
import com.whl.org.domain.ShopAuditLog;
import com.whl.org.dto.ShopDto;
import com.whl.org.query.ShopQuery;
import com.whl.org.service.IShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 接口类：店铺
 */
@RestController
@RequestMapping("/shop")
@Api(value = "店铺接口Api", description = "店铺接口的CRUD方法")
public class ShopController {

    @Autowired
    private IShopService shopService;

    /**
     * 接口：查询单个对象
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "查询单个对象")
    public Shop queryById(@PathVariable("id") Long id){
        return shopService.findOne(id);
    }

    /**
     * 接口：查询所有对象
     * @return
     */
    @GetMapping
    @ApiOperation(value = "查询所有对象")
    public List<Shop> queryAll(){
        return shopService.findAll();
    }

    /**
     * 接口：分页和高级查询
     * @param query
     * @return
     */
    @PostMapping
    @ApiOperation(value = "分页和高级查询")
    public PageList<Shop> queryPage(@RequestBody ShopQuery query){
        return shopService.queryPage(query);
    }

    /**
     * 接口：删除
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            shopService.delete(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
     * 接口：添加和修改
     * @param shop
     * @return
     */
    @PutMapping
    @ApiOperation(value = "添加和修改")
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        try {
            if (shop.getId() == null){ // 添加操作
                shopService.add(shop);
            }else { // 修改操作
                shopService.update(shop);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"操作失败");
        }
    }

    /**
     * 接口：批量删除
     * @param ids
     * @return
     */
    @PatchMapping
    @ApiOperation(value = "批量删除")
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            shopService.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"删除失败");
        }
    }

    /**
     * 接口：店铺入驻
     * @param shop
     * @return
     */
    @PostMapping("/settlement")
    @ApiOperation(value = "店铺入驻")
    public AjaxResult settlement(@RequestBody Shop shop){
        try {
            shopService.settlement(shop);
            return new AjaxResult();
        } catch (BusinessException e) { // 业务异常捕获
            e.printStackTrace();
            return new AjaxResult(false,"入驻失败" + e.getMessage());
        } catch (Exception e) { // 其他异常
            e.printStackTrace();
            return new AjaxResult(false,"系统繁忙，请稍后重试!!" + e.getMessage());
        }
    }

    /**
     * 接口：店铺入驻审核通过
     * @param shopAuditLog
     * @return
     */
    @PostMapping("/audit/pass")
    @ApiOperation(value = "店铺入驻审核通过")
    public AjaxResult pass(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopService.pass(shopAuditLog); // 审核通过
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"审核失败");
        }
    }

    /**
     * 接口：店铺入驻审核驳回
     * @param shopAuditLog
     * @return
     */
    @PostMapping("/audit/reject")
    @ApiOperation(value = "店铺入驻审核驳回")
    public AjaxResult reject(@RequestBody ShopAuditLog shopAuditLog){
        try {
            shopService.reject(shopAuditLog); // 审核驳回
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"驳回失败");
        }
    }

    /**
     * 导出数据
     * @param response
     */
    @GetMapping("/export")
    public void export(HttpServletResponse response){
        List<Shop> shops = shopService.findAll();
        /**
         * shops - 导出的数据
         * null - 标题，一般无
         * "店铺信息" - sheet页的名称
         * Shop.class - 导出的类型
         * "shop.xlsx" - 导出文件名
         * response - 导出时的响应对象
         */
        ExcelUtil.exportExcel(shops,null,"店铺信息",Shop.class,"shop.xlsx",response);
    }

    /**
     * 导入数据：前端要上传
     * @param file
     */
    @PostMapping("/importExcel")
    public void importExcel(@RequestPart MultipartFile file){
        /**
         * file - 要导入的文件 - 前端上传的文件
         * 0 - 标题行数 - 没有
         * 1 - 头函数 = 字段名
         * Shop.class = 导入时映射的对象
         */
        List<Shop> shops = ExcelUtil.importExcel(file, 0, 1, Shop.class);
        shops.forEach(System.out::println);
    }

    /**
     * 导出
     * @param
     */
    @GetMapping("/echarts")
    public List<ShopDto> getCountByState(){
       List<ShopDto> shopDtos = shopService.getCountByState();
       return shopDtos;
    }

    /**
     * 店铺激活
     * @param
     */
    @GetMapping("/active/{id}")
    public AjaxResult shopActive(@PathVariable("id") Long id){
        try {
            shopService.shopActive(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult(false,"激活失败");
        }
    }

}
